package com.nit.tools.doc;

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class WordTemplateFiller {
    public static void main(String[] args) {
        try {
            // 加载Word模板文件
            XWPFDocument document = new XWPFDocument(new FileInputStream("D://template.docx"));

            // 获取所有段落
            List<XWPFParagraph> paragraphs = document.getParagraphs();

            // 遍历所有段落
            for (XWPFParagraph paragraph : paragraphs) {
                List<XWPFRun> runs = paragraph.getRuns();

                // 遍历段落中的所有文本内容
                for (XWPFRun run : runs) {
                    String text = run.getText(0);

                    // 根据占位符替换内容
                    if (text != null && text.contains("{{NAME}}")) {
                        text = text.replace("{{NAME}}", "张三");
                        run.setText(text, 0);
                    }

                    if (text != null && text.contains("{{AGE}}")) {
                        text = text.replace("{{AGE}}", "25");
                        run.setText(text, 0);
                    }

                    if (text != null && text.contains("{{EMAIL}}")) {
                        text = text.replace("{{EMAIL}}", "zhangsan@example.com");
                        run.setText(text, 0);
                    }
                }
            }

            // 获取文档中的表格并替换表格中的占位符（变量）为实际的数据
            for (XWPFTable table : document.getTables()) {
                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        replacePlaceholder(cell, "{{AGE}}", "撒大苏打.d123213oe@example.com");
                        replacePlaceholder(cell, "{{NAME}}", "zhangsan.doe@example.com");
                        replacePlaceholder(cell, "{{C2", "cccccccccccexample");                    }
                }
            }


            // 保存填充后的Word文档
            FileOutputStream outputStream = new FileOutputStream("D://output.docx");
            document.write(outputStream);
            outputStream.close();

            System.out.println("Word文档生成成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void replacePlaceholder(XWPFRun run, String placeholder, String replacement) {
        String text = run.getText(0);
        if (text != null && text.contains(placeholder)) {
            text = text.replace(placeholder, replacement);
            run.setText(text, 0);
        }
    }

    private static void replacePlaceholder(XWPFParagraph paragraph, String placeholder, String replacement) {
        for (XWPFRun run : paragraph.getRuns()) {
            replacePlaceholder(run, placeholder, replacement);
        }
    }

    private static void replacePlaceholder(XWPFTableCell cell, String placeholder, String replacement) {
        for (XWPFParagraph paragraph : cell.getParagraphs()) {
            replacePlaceholder(paragraph, placeholder, replacement);
        }
    }
}